package Client;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.SliderUI;
/**
 * GUI for Matching Game
 * @author Termchai 5510546042, Eknarin 5510546239
 *
 */
public class GUI {
	
	private JFrame frame;
	private CardComponent[][] cc;
	private static GameConsole gameConsole;
	private JPanel[] panel;
	private JPanel bigPanel;
	private JLabel[] score;
	private JLabel status;
	private Turn player;
	private Client client;
	private int tempFixScoreLabel;
	

	public GUI(GameConsole gameConsole, Turn player, Client client) throws Exception
	{
		this.player=player;
		this.gameConsole=gameConsole;
		this.client=client;
		frame = new JFrame("Matching Game");
		frame.setPreferredSize(new Dimension (700,600));
		frame.setDefaultCloseOperation(3);
		frame.setBackground(new Color(74,121,165));
		tempFixScoreLabel = 0;
		initComponent();
		run();
	}

	public Card getCard(int[] x)
	{
		return cc[x[0]][x[1]].getCard();
	}
	
	public Turn getPlayer()
	{
		return player;
	}
	
	/**
	 * clear selected card component
	 */
	public void clear()
	{
		for (CardComponent[] temp : cc)
		{
			for (CardComponent tempp : temp)
			{
				if (tempp.getSelect()!=2)
				{
					tempp.setSelect(0);
					CardComponent.clearSelect();
				}
			}
		}
		CardComponent.count=0;
	}
	/**
	 * clear when restart game
	 */
	public void clearNew()
	{
		for (CardComponent[] temp : cc)
		{
			for (CardComponent tempp : temp)
			{
				tempp.setSelect(0);
				CardComponent.clearSelect();
				tempp.setSelect(0);
			}
		}
	}
	
	/**
	 * plus one score
	 * @param x index player
	 */
	public void plusScore(int x)
	{
		gameConsole.getInfo().plusScore(x);
		score[x].setText(gameConsole.getInfo().getName()[x] + " : " + gameConsole.getInfo().getScore()[x]);
	}
	/**
	 * initComponent
	 */
	public void initComponent()
	{ 
		score = new JLabel[2];
		
		score[0] = new JLabel (gameConsole.getInfo().getName()[0] + " : 0");
		score[0].setPreferredSize(new Dimension(125,50));
		score[0].setFont(new Font("Serif", 0, 20));
		score[0].setForeground(Color.red);
		
		score[1] = new JLabel (gameConsole.getInfo().getName()[1] + " : 0");
		score[1].setFont(new Font("Serif", 0, 20));
		score[1].setPreferredSize(new Dimension(125,50));
		score[1].setForeground(Color.blue);
		
		
		status = new JLabel ("Turn : " + gameConsole.getTurn());
		status.setFont(new Font("Serif", 0, 40));
		status.setPreferredSize(new Dimension(180,60));
		status.setForeground(Color.red);
		status.setBorder(new TitledBorder("Status"));
		
		panel = new JPanel[6];
		bigPanel = new JPanel();
		bigPanel.setLayout(new GridLayout(6,1));
		cc = new CardComponent[5][6];
		//match.addActionListener(new MatchAction(this));
		
		
		createPanel();
		panel[5].add(score[0]);
		panel[5].add(score[1]);
		panel[5].add(status);
		cardToLabel(gameConsole.getGame().getDeck());
		addLabel();
		

		frame.add(bigPanel);
		
	}
	
	public CardComponent[][] getCC()
	{
		return cc;
	}
	
	/**
	 * loop for create panel
	 */
	public void createPanel()
	{
		for (int i=0; i<6; i++)
		{
			panel[i]=new JPanel();
			bigPanel.add(panel[i]);
		}
	}
	
	public GameConsole getGameConsole()
	{
		return gameConsole;
	}
	
	/**
	 * loop for add label
	 */
	public void addLabel()
	{
		for (int i=0; i<5; i++)
		{
			for (int j=0; j<6; j++)
			{
				panel[i].add(cc[i][j]);
				cc[i][j].setPreferredSize(new Dimension(70,93));
			}
		}
	}
	
	/**
	 * add card to CardComponent
	 * @param card deck card from Game
	 */
	public void cardToLabel(Card[][] card)
	{
		int x=1;
		for (int i=0; i<5; i++)
			for (int j=0; j<6; j++)
			{
				cc[i][j] = new CardComponent("",card[i][j],this,client,gameConsole);
			}
	}
	
	/**
	 * make JFrame visible
	 */
	public void run()
	{
		frame.pack();
		frame.setVisible(true);
	}
	
	/**
	 * update label turn and score
	 */
	public void updateTurn()
	{
		if (gameConsole.getTurn()==Turn.P1)
		{
			status.setText("Turn : " + gameConsole.getTurn());
			status.setForeground(Color.red);
		}
		else if (gameConsole.getTurn()==Turn.P2){
			status.setText("Turn : " + gameConsole.getTurn());
			status.setForeground(Color.blue);
		}
		else if (gameConsole.getTurn()==Turn.Wait)
		{
			status.setText("WAIT");
		}
		
		
		if (gameConsole.getInfo().getScore()[0]+gameConsole.getInfo().getScore()[1] == 15 && tempFixScoreLabel==0)
		{
			tempFixScoreLabel++;
			if (gameConsole.getInfo().getScore()[0]>gameConsole.getInfo().getScore()[1])
				JOptionPane.showMessageDialog(frame,gameConsole.getInfo().getName()[0] + " Win!!!");
			else
				JOptionPane.showMessageDialog(frame,gameConsole.getInfo().getName()[1] + " Win!!!");
			
		}
		System.out.println("Score 1: " + gameConsole.getInfo().getScore()[0]);
		System.out.println("Score 2: " + gameConsole.getInfo().getScore()[1]);
	}
	
	
	/**
	class MatchAction extends AbstractAction
	{

		GUI gui;
		
		public MatchAction(GUI gui)
		{
			this.gui=gui;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub

			
			if (CardComponent.getCardSelect(1)!=null && CardComponent.getCardSelect(2)!=null)
			{
				System.out.println("Card 1 : " + gui.getCard(CardComponent.getCardSelect(1)).getValue());
				System.out.println("Card 2 : " + gui.getCard(CardComponent.getCardSelect(2)).getValue());
				if (gui.getCard(CardComponent.getCardSelect(1)).getValue() == gui.getCard(CardComponent.getCardSelect(2)).getValue())
				{
					if (gameConsole.getTurn()==Turn.P1)
					{
						gui.plusScore(0);
					}
					else if (gameConsole.getTurn()==Turn.P2)
					{
						gui.plusScore(1);
					}
					gui.getCC() [CardComponent.getCardSelect(1)[0]] [CardComponent.getCardSelect(1)[1]].setSelect(2);
					gui.getCC() [CardComponent.getCardSelect(2)[0]] [CardComponent.getCardSelect(2)[1]].setSelect(2);
					gui.clear();
				}
				else 
				{
					if (gameConsole.getTurn()==Turn.P1)
					{
						gameConsole.setTurn(Turn.P2);
					}
					else if (gameConsole.getTurn()==Turn.P2)
					{
						gameConsole.setTurn(Turn.P1);
					}
					gui.updateTurn();
					
					gui.getCC() [CardComponent.getCardSelect(1)[0]] [CardComponent.getCardSelect(1)[1]].setSelect(2);
					gui.getCC() [CardComponent.getCardSelect(2)[0]] [CardComponent.getCardSelect(2)[1]].setSelect(2);

					Timer timer = new Timer(2500, new FaceUpAction(gui));
					timer.setRepeats(false);
					timer.start();
				}
			}
		}
		
	}
	*/
	
	
}

